package com.xzxy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzxy.xmlg_backend.entity.Result;
import com.xzxy.xmlg_backend.entity.User;
import com.xzxy.xmlg_backend.service.IUserService;
import com.xzxy.xmlg_backend.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理相关接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "分页查询用户信息")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String username) {
        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!"".equals(username)) {
            queryWrapper.like("username", username);
        }
        return Result.success(userService.page(page, queryWrapper));
    }

    @PostMapping
    @Operation(summary = "新增或更新用户", description = "新增或更新用户信息")
    public Result save(@RequestBody User user) {
        if (user.getId() == null) {
            user.setCreateTime(LocalDateTime.now());
        }
        user.setUpdateTime(LocalDateTime.now());
        userService.saveOrUpdate(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户信息")
    public Result delete(@PathVariable Integer id) {
        userService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    @Operation(summary = "批量删除用户", description = "批量删除用户信息")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    @Operation(summary = "查询所有用户", description = "查询所有用户信息")
    public Result findAll() {
        return Result.success(userService.list());
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户", description = "根据ID查询用户信息")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }

    @PostMapping("/login")
    @Operation(summary = "登录", description = "用户登录")
    public Result login(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            HashMap<String, Object> userDetail = new HashMap<>();
            userDetail.put("username",one.getUsername());
            userDetail.put("role",one.getRole());
            userDetail.put("avatar",one.getAvatar());
            String token = JwtUtils.generateJwt(userDetail);
            return Result.success(token);
        } else {
            return Result.fail("用户名或密码错误");
        }
    }
}